package com.zhou.jwt;


import com.zhou.Properties.JwtProperties;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
// 教师登录及权限验证 邹
@WebFilter({"/teacher/*"})
public class TeacherJwtFilter implements Filter {
    @Autowired
    JwtProperties jwtProperties;
	//主方法，当有符合要求的请求时都会被拦截并执行此方法
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        //ServletRequest和servletRequest可以强转成HttpServletRequest、HttpServletResponse
        //很多功能只有他们才能做
        HttpServletRequest httpServletRequest = (HttpServletRequest) servletRequest;
        String header = httpServletRequest.getHeader(jwtProperties.getName());
        Claims jjwt = Jwts.parser()
                .setSigningKey(jwtProperties.getSignature()) //指定签名秘钥
                .parseClaimsJws(header)
                .getBody();
        Object type = jjwt.get("type");//拿取指定数据 name
        System.out.println(jjwt);
        if ((int)type == 1) filterChain.doFilter(servletRequest,servletResponse);  //放行
    }

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
    }

    @Override
    public void destroy() {}

}